package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.Result;
import com.itheima.youdianda.domain.SysUser;
import com.itheima.youdianda.dto.SysUserDto;
import com.itheima.youdianda.service.SysRoleService;
import com.itheima.youdianda.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chensen
 * @create 2022/9/7/14:59
 */
@RestController
@RequestMapping("sys_user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;


    @GetMapping("list")
    public Result list(String login_name, String status, String phone, Integer page, Integer limit) {
        Page<SysUserDto> userPage = sysUserService.pageWithDto(login_name, status, phone, page, limit);
        Map<String, Object> map = new HashMap<>();
        map.put("count", userPage.getTotal());
        map.put("total_pages", userPage.getPages());
        map.put("page_size", userPage.getSize());
        map.put("current_page", userPage.getCurrent());
        map.put("data", userPage.getRecords());
        return Result.ok(map);
    }

    @GetMapping("del")
    public Result delete(String id) {
        sysUserService.removeById(id);
        return Result.ok("删除成功！");
    }

    @PostMapping("add")
    public Result add(@RequestBody SysUserDto sysUserDto) {
        sysUserService.saveWithDto(sysUserDto);
        return Result.ok("添加成功！");
    }

    @GetMapping("rowInfo")
    public Result rowInfo(String id) {
      SysUserDto sysUserDto=sysUserService.getByIdWithRoleId(id);
        return Result.ok(sysUserDto);
    }

    @PostMapping("update")
    public Result update(@RequestBody SysUserDto sysUserDto) {
        sysUserService.updateWithDto(sysUserDto);
        return Result.ok("修改成功！");

    }


}
